<?php

namespace app\common\model\qa;

use app\common\library\Utils;
use think\Db;
use think\Model;

class Question extends Model
{

    protected $name = 'parent_questions';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;

    //时间获取器
    public function getCreatetimeAttr($value)
    {
        $time = Utils::time_format($value);
        return $time;
    }

    //问题的答案数量
    public function getCountTextAttr($value, $data) {
        $count = Db::name('parent_answers')->where(['parent_questions_id'=> $data['id']])->count();

        return $count;
    }

    //获取最新的一条回复
    public function getNewInfoAttr($value, $data) {
        $new = Db::name('parent_answers')->where(['parent_questions_id'=> $data['id']])->order('createtime desc')->find();
        return $new;
    }

}
